<template>
  <n-drawer v-model:show="isDrawer" :width="width" :auto-focus="false" @after-leave="handleReset">
    <n-drawer-content :title="title" closable>
      <n-form
        :model="formParams"
        :rules="rules"
        ref="formRef"
        label-placement="left"
        :label-width="140"
      >
        <n-form-item label="名称" path="title">
          <n-input placeholder="请输入名称" v-model:value="formParams.title" />
        </n-form-item>

        <n-form-item label="描述" path="description">
          <n-input placeholder="请输入描述内容" v-model:value="formParams.description" />
        </n-form-item>
      </n-form>

      <template #footer>
        <n-space>
          <n-button @click="handleReset">重置</n-button>
          <n-button type="primary" :loading="subLoading" @click="formSubmit">提交</n-button>
        </n-space>
      </template>

    </n-drawer-content>
  </n-drawer>
</template>

<script lang="ts" setup>
  import { ref, onMounted } from 'vue';
  import { useMessage, FormRules } from 'naive-ui';
  import { addboxType, editboxType, typeboxInfo } from '@/api/device/type';

  const rules: FormRules = {
    title: {
      required: true,
      message: '名称不能为空',
      trigger: 'blur',
    }
  };

  const emit = defineEmits(['change']);

  const props = defineProps({
    title: {
      type: String,
      default: '添加类型',
    },
    width: {
      type: Number,
      default: 850,
    },
    roleList: {
      type: Array,
    },
  });

  const defaultValueRef = () => ({
    id: null,
    title: '',
    description: '',
  });

  const message = useMessage();
  const formRef: any = ref(null);
  const isDrawer = ref(false);
  const subLoading = ref(false);
  const formParams = ref(defaultValueRef());

  async function openDrawer(id?) {
    if (id) {
      formParams.value.id = id;
      getInfo();
      return;
    }
    isDrawer.value = true;
  }

  function closeDrawer() {
    isDrawer.value = false;
  }

  function formSubmit() {
    formRef.value.validate((errors) => {
      if (!errors) {
        const msg = formParams.value.id ? '编辑成功' : '添加成功';
        if (formParams.value.id) {
          editboxType(formParams.value).then((_) => {
            message.success(msg);
            emit('change');
            handleReset();
            closeDrawer();
          });
        } else {
          addboxType(formParams.value).then((_) => {
            message.success(msg);
            emit('change');
            handleReset();
            closeDrawer();
          });
        }
      } else {
        message.error('请填写完整信息');
      }
    });
  }

  function handleReset() {
    formRef.value.restoreValidation();
    formParams.value = Object.assign(formParams.value, defaultValueRef());
  }

  function getInfo() {
    typeboxInfo({ id: formParams.value.id }).then((res) => {
      const params = {
        id: formParams.value.id,
        title: res.title,
        description: res.description,
      };

      formParams.value = Object.assign(formParams.value, params);
      isDrawer.value = true;
    });
  }

  onMounted(async () => {

  });

  defineExpose({
    openDrawer,
    closeDrawer,
  });
</script>
